<?php


class GoodstrunkApp extends BackendApp
{
	var $_goodstrunk_mod;
	var $_goodstrunksub_mod;
	function __construct()
	{
		$this->GoodstrunkApp();
	}

	function GoodstrunkApp()
	{
		parent::BackendApp();
		$this->_goodstrunk_mod =& m('goodstrunk');
		$this->_goodstrunksub_mod =& m('goodstrunksub');


	}

	function index()
	{
		$conditions = "1=1 ";
		if($_GET['g_code']!=''){
			$conditions.=" and g_code like '%".$_GET['g_code']."%'";
		}
		if($_GET['g_awb_code']!=''){
			$conditions.=" and g_awb_code like '%".$_GET['g_awb_code']."%'";
		}
		if($_GET['g_voyage']!=''){
			$conditions.=" and g_voyage like '%".$_GET['g_voyage']."%'";
		}
		if($_GET['g_ship']!=''){
			$conditions.=" and g_ship like '%".$_GET['g_ship']."%'";
		}
		$user_ids = $this->checkpower();
		if(!empty($user_ids)){
			$conditions.=" and g_optid in(".$user_ids.")";
		}
		$page   =   $this->_get_page(10);   //获取分页信息

		$sql="SELECT n.*,m.user_name,IFNULL(x.ws_tid,0) as ws_tid FROM zys_goods_trunk n LEFT JOIN zys_member m ON n.g_optid=m.user_id
		LEFT JOIN (SELECT ws_tid,IFNULL(ws_gid,0) as ws_gid from zys_waybill_sub_trunk GROUP BY ws_gid) x on n.g_tid=ws_gid
		where $conditions ORDER BY g_opdate DESC limit $page[limit]";

			
		$goodstrunk=$this->_goodstrunk_mod->getAll($sql);

		$sql="select count(1) from zys_goods_trunk where $conditions";
		$page['item_count']=$this->_goodstrunk_mod->getOne($sql);   //获取统计数据


		$this->_format_page($page);
		$this->assign('page_info', $page);   //将分页信息传递给视图，用于形成分页条
		$this->assign('auth', $goodstrunk);
		$this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
                                      'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
		$this->assign('tabself', json_encode(array('text'=>'货单管理')));
		$this->display('goodstrunk.index.html');
	}



	//新增
	function add(){
		if (!IS_POST){
			$goodstrunk=array();
			$goodstrunk['g_code']='T'.gmtime();
			$goodstrunk['user_name']=$this->visitor->get('user_name');
			$this->assign('auth', $goodstrunk);
			$types=array();
			$types['集港'] = '集港';
			$types['疏港'] = '疏港';
			$this->assign('type_options', $types);
			$db=&db();
			$sq2="SELECT m.zmm_otid,m.zmm_name  FROM zys_monitor_main m";//监管方案
			$typelist=$db->getall($sq2);
			foreach ($typelist as $v){
				$monitor_main[$v['zmm_otid']]=$v['zmm_name'];
			}
			$this->assign('monitor_main', $monitor_main);
			/* 导入jQuery的表单验证插件 */
			$this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js,jquery.plugins/jquery.validate.js',
        'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
			$this->assign('isbool',1);//等于1显示提交按钮等等
			$this->assign('tabself', json_encode(array('text'=>'新建货单')));
			$this->assign('tabback', json_encode(array('text'=>'货单管理','url'=>'')));
			$this->display('goodstrunk.form.html');
		}else{
			if(empty($_POST['g_ship']) || empty($_POST['g_voyage']) || empty($_POST['g_awb_code']) || empty($_POST['g_packingtime']) || empty($_POST['g_collectiontime'])
			|| empty($_POST['g_container_depot']) || empty($_POST['g_container_man']) || empty($_POST['g_container_tel']) || empty($_POST['g_full_container_depot']) || empty($_POST['g_full_container_man'])
			|| empty($_POST['g_full_container_tel']) || empty($_POST['g_loading_place']) || empty($_POST['g_loading_man']) || empty($_POST['g_loading_tel']) || empty($_POST['g_supervise'])
			){
				$this->show_warning('信息填写不完整', '', 'index.php?app=goodstrunk&act=add');
			}
			$data = array(
				 'g_code'  => $_POST['g_code'],
				 'g_opdate'   => gmtime(),
				 'g_status'   => '已提交',
				 'g_optid'   => $this->visitor->get('user_id'),
				 'g_opname'   => $this->visitor->get('user_name'),
				 'g_companytid'   => $_POST['g_companytid'],
				 'g_company'   => $_POST['hzshow'],
				 'g_type'  => $_POST['g_type'],
				 'g_note'      => $_POST['g_note'],
				 'g_ship'      => $_POST['g_ship'],
				 'g_voyage'      => $_POST['g_voyage'],
				 'g_awb_code'      => $_POST['g_awb_code'],
				 'g_container_depot'	     => $_POST['g_container_depot'],
				 'g_container_lng'		 =>$_POST['g_container_lng'],
				 'g_container_lat'		 =>$_POST['g_container_lat'],
				 'g_container_man'		 =>$_POST['g_container_man'],
				 'g_container_tel'		 =>$_POST['g_container_tel'],
				 'g_full_container_depot'      => $_POST['g_full_container_depot'],
				 'g_full_container_lng'        => $_POST['g_full_container_lng'],
				 'g_full_container_lat'        => $_POST['g_full_container_lat'],
				 'g_full_container_man'        => $_POST['g_full_container_man'],
				 'g_full_container_tel'        => $_POST['g_full_container_tel'],
				 'g_loading_lng'      => $_POST['g_loading_lng'],
				 'g_loading_lat'      => $_POST['g_loading_lat'],
				 'g_loading_man'	   => $_POST['g_loading_man'],
				 'g_supervise'	     => $_POST['g_supervise'],
				 'g_loading_place'	     => $_POST['g_loading_place'],
				 'g_loading_tel'	     => $_POST['g_loading_tel'],
				 'g_packingtime'	     => gmstr2time($_POST['g_packingtime']),
				 'g_collectiontime'	     => gmstr2time($_POST['g_collectiontime']),
			);

			/* 保存 */
			$rows = $this->_goodstrunk_mod->add($data);
			if ($this->_goodstrunk_mod->has_error())
			{
				$this->show_warning($this->_goodstrunk_mod->get_error(), '',   'index.php?app=goodstrunk&act=add');
				return;
			}
			$userid = $this->visitor->get('user_id');
			$unit = $this->visitor->get('unit');
			if($_POST['g_type']=='集卡进口'){
				$num = $this->checkaddr($_POST['g_container_depot'],$userid,$unit);
				if($num==0){
					$depot = array(
						'user_id'    	=>	$userid,
						'consignee' 	=>	$_POST['g_container_man'],
						'address' 		=>	$_POST['g_container_depot'],
						'phone_tel'	=>	$_POST['g_container_tel'],
						'lng'			=>	$_POST['g_container_lng'],
						'lat'			=>	$_POST['g_container_lat'],
						'addrtype'		=>	'集进空站',
						'unit'			=>	$unit,
					);
					$this->addaddr($depot);
				}
				$num1 = $this->checkaddr($_POST['g_full_container_depot'],$userid,$unit);
				if($num1==1){
					$full_depot = array(
						'user_id'    	=>	$userid,
						'consignee' 	=>	$_POST['g_full_container_man'],
						'address' 		=>	$_POST['g_full_container_depot'],
						'phone_tel'	=>	$_POST['g_full_container_tel'],
						'lng'			=>	$_POST['g_full_container_lng'],
						'lat'			=>	$_POST['g_full_container_lat'],
						'addrtype'		=>	'集进重站',
						'unit'			=>	$unit,
					);
					$this->addaddr($full_depot);
				}
				$num2 = $this->checkaddr($_POST['g_loading_place'],$userid,$unit);
				if($num2==0){
					$place = array(
						'user_id'    	=>	$userid,
						'consignee' 	=>	$_POST['g_loading_man'],
						'address' 		=>	$_POST['g_loading_place'],
						'phone_tel'	=>	$_POST['g_loading_tel'],
						'lng'			=>	$_POST['g_loading_lng'],
						'lat'			=>	$_POST['g_loading_lat'],
						'addrtype'		=>	'集进卸货',
						'unit'			=>	$unit,
					);
					$this->addaddr($place);
				}

			}else{
				$num = $this->checkaddr($_POST['g_container_depot'],$userid,$unit);
				if($num==0){
					$depot = array(
						'user_id'    	=>	$userid,
						'consignee' 	=>	$_POST['g_container_man'],
						'address' 		=>	$_POST['g_container_depot'],
						'phone_tel'	=>	$_POST['g_container_tel'],
						'lng'			=>	$_POST['g_container_lng'],
						'lat'			=>	$_POST['g_container_lat'],
						'addrtype'		=>	'集出空站',
						'unit'			=>	$unit,
					);
					$this->addaddr($depot);
				}
				$num1 = $this->checkaddr($_POST['g_full_container_depot'],$userid,$unit);
				if($num1==0){
					$full_depot = array(
						'user_id'    	=>	$userid,
						'consignee' 	=>	$_POST['g_full_container_man'],
						'address' 		=>	$_POST['g_full_container_depot'],
						'phone_tel'	=>	$_POST['g_full_container_tel'],
						'lng'			=>	$_POST['g_full_container_lng'],
						'lat'			=>	$_POST['g_full_container_lat'],
						'addrtype'		=>	'集出重站',
						'unit'			=>	$unit,
					);
					$this->addaddr($full_depot);
				}
				$num2 = $this->checkaddr($_POST['g_loading_place'],$userid,$unit);
				if($num2==0){
					$place = array(
						'user_id'    	=>	$userid,
						'consignee' 	=>	$_POST['g_loading_man'],
						'address' 		=>	$_POST['g_loading_place'],
						'phone_tel'	=>	$_POST['g_loading_tel'],
						'lng'			=>	$_POST['g_loading_lng'],
						'lat'			=>	$_POST['g_loading_lat'],
						'addrtype'		=>	'集出卸货',
						'unit'			=>	$unit,
					);
					$this->addaddr($place);
				}
			}
			$this->show_message('添加成功',
                '返回货单',   'index.php?app=goodstrunk&act=edit&id=' . $rows
			);
		}
	}



	//编辑
	function edit()
	{

		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);

		if (!IS_POST){
			$conditions=" 1 =1 and g_tid=".$id;
			$sql="SELECT n.*,m.user_name,o.o_linkman FROM zys_goods_trunk n
LEFT JOIN zys_member m ON n.g_optid=m.user_id
LEFT JOIN zys_operator o ON n.g_optid=o.o_tid AND o.o_consignor=1
			where $conditions  limit 1";
			$goodstrunk=$this->_goodstrunk_mod->getRow($sql);
			$this->assign('id', $id);
			$sql2="SELECT n.*,IFNULL(n.g_volume-n.g_volumep,0) as g_plus FROM zys_goods_trunk_sub n WHERE n.gs_gtid=".$id;
			$goodstrunksub=$this->_goodstrunksub_mod->getAll($sql2);
			$boxtype=array();
			$boxtype['20GP']='20GP';
			$boxtype['40GP']='40GP';
			$boxtype['20RF']='20RF';
			$boxtype['40RF']='40RF';
			$boxtype['40RH']='40RH';
			$this->assign('box_options', $boxtype);
			$this->assign('trunk', $goodstrunksub);
			$this->assign('isbool',1);//等于1显示提交按钮等等
			$this->assign('auth', $goodstrunk);
			$db=&db();
			$sq2="SELECT m.zmm_otid,m.zmm_name  FROM zys_monitor_main m";//监管方案
			$typelist=$db->getall($sq2);
			$sql="select 1 from zys_waybill_sub_trunk where ws_gid=".$id;
			if($db->getOne($sql)){
				$this->assign('isbool',0);//等于1显示提交按钮等等
			}
			foreach ($typelist as $v){
				$monitor_main[$v['zmm_otid']]=$v['zmm_name'];
			}
			$this->assign('monitor_main', $monitor_main);
			//是否有已拆分货物
			$sql = "select * from zys_goods_trunk_sub where g_gstid!='' and gs_gtid =".$id;
			$remove = $db->getAll($sql);
			$this->assign('remove', $remove);
			/* 导入jQuery的表单验证插件 */
			$this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js,jquery.plugins/jquery.validate.js',
        'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
			$this->assign('tabself', json_encode(array('text'=>'货单编辑')));
			$this->assign('tabback', json_encode(array('text'=>'货单管理','url'=>'')));
			$this->display('goodstrunk.form.html');
		}else
		{
			$trunk = $_POST['students'];
			if ($trunk){
				foreach ($trunk as $k=>$v) {
					if($v['g_box']!=''){
						$data = array(
							'g_box'      => $v['g_box'],
							'g_volume'      => $v['g_volume'],
							'g_ctn_no'      => $v['g_ctn_no'],
							'g_eir_code'   =>     $v['g_eir_code'],
							'g_viewer'   =>     $_POST['g_companytid']
						);
					}
					if ($v['gs_tid']==''||$v['gs_tid']==null){
						$data['gs_gtid']=$id;
						$rows = $this->_goodstrunksub_mod->add($data);
					}else {

						$rows = $this->_goodstrunksub_mod->edit($v['gs_tid'], $data);
					}
				};
			}
			if(empty($_POST['g_ship']) || empty($_POST['g_voyage']) || empty($_POST['g_awb_code']) || empty($_POST['g_packingtime']) || empty($_POST['g_collectiontime'])
			|| empty($_POST['g_container_depot']) || empty($_POST['g_container_man']) || empty($_POST['g_container_tel']) || empty($_POST['g_full_container_depot']) || empty($_POST['g_full_container_man'])
			|| empty($_POST['g_full_container_tel']) || empty($_POST['g_loading_place']) || empty($_POST['g_loading_man']) || empty($_POST['g_loading_tel']) || empty($_POST['g_supervise'])
			){
				$this->show_warning('信息填写不完整', '', 'index.php?app=goodstrunk&act=edit&id=' . $id);
			}
			$data = array(
				'g_companytid'   => $_POST['g_companytid'],
				'g_company'   => $_POST['hzshow'],
      			'g_note'      => $_POST['g_note'],
			    'g_ship'      => $_POST['g_ship'],
				'g_voyage'      => $_POST['g_voyage'],
				'g_awb_code'      => $_POST['g_awb_code'],
				'g_container_depot'	     => $_POST['g_container_depot'],
				'g_container_lng'		 =>$_POST['g_container_lng'],
				'g_container_lat'		 =>$_POST['g_container_lat'],
				'g_container_man'		 =>$_POST['g_container_man'],
				'g_container_tel'		 =>$_POST['g_container_tel'],
				'g_full_container_depot'      => $_POST['g_full_container_depot'],
				'g_full_container_lng'        => $_POST['g_full_container_lng'],
				'g_full_container_lat'        => $_POST['g_full_container_lat'],
				'g_full_container_man'        => $_POST['g_full_container_man'],
				'g_full_container_tel'        => $_POST['g_full_container_tel'],
				'g_supervise'	     => $_POST['g_supervise'],
				'g_loading_tel'      => $_POST['g_loading_tel'],
				'g_loading_man'      => $_POST['g_loading_man'],
			    'g_loading_place'	     => $_POST['g_loading_place'],
			    'g_loading_lng'	     => $_POST['g_loading_lng'],
			    'g_loading_lat'	     => $_POST['g_loading_lat'],
			    'g_packingtime'	     => gmstr2time($_POST['g_packingtime']),
			    'g_collectiontime'	     => gmstr2time($_POST['g_collectiontime']),
			);
			$sql="SELECT g_container_depot,g_full_container_depot,g_loading_place FROM zys_goods_trunk where g_tid=".$id;
			$goods=$this->_goodstrunk_mod->getRow($sql);
			$userid = $this->visitor->get('user_id');
			$unit = $this->visitor->get('unit');
			if($goods['g_type']=='集卡进口'){
				$num = $this->checkaddr($_POST['g_container_depot'],$userid,$unit);
				if($num==0){
					$depot = array(
						'user_id'    	=>	$userid,
						'consignee' 	=>	$_POST['g_container_man'],
						'address' 		=>	$_POST['g_container_depot'],
						'phone_tel'	=>	$_POST['g_container_tel'],
						'lng'			=>	$_POST['g_container_lng'],
						'lat'			=>	$_POST['g_container_lat'],
						'addrtype'		=>	'集进空站',
						'unit'			=>	$unit,
					);
					$this->addaddr($depot);
				}
				$num1 = $this->checkaddr($_POST['g_full_container_depot'],$userid,$unit);
				if($num1==0){
					$full_depot = array(
						'user_id'    	=>	$userid,
						'consignee' 	=>	$_POST['g_full_container_man'],
						'address' 		=>	$_POST['g_full_container_depot'],
						'phone_tel'	=>	$_POST['g_full_container_tel'],
						'lng'			=>	$_POST['g_full_container_lng'],
						'lat'			=>	$_POST['g_full_container_lat'],
						'addrtype'		=>	'集进重站',
						'unit'			=>	$unit,
					);
					$this->addaddr($full_depot);
				}
				$num2 = $this->checkaddr($_POST['g_loading_place'],$userid,$unit);
				if($num2==0){
					$place = array(
						'user_id'    	=>	$userid,
						'consignee' 	=>	$_POST['g_loading_man'],
						'address' 		=>	$_POST['g_loading_place'],
						'phone_tel'	=>	$_POST['g_loading_tel'],
						'lng'			=>	$_POST['g_loading_lng'],
						'lat'			=>	$_POST['g_loading_lat'],
						'addrtype'		=>	'集进卸货',
						'unit'			=>	$unit,
					);
					$this->addaddr($place);
				}

			}else{
				$num = $this->checkaddr($_POST['g_container_depot'],$userid,$unit);
				if($num==0){
					$depot = array(
						'user_id'    	=>	$userid,
						'consignee' 	=>	$_POST['g_container_man'],
						'address' 		=>	$_POST['g_container_depot'],
						'phone_tel'	=>	$_POST['g_container_tel'],
						'lng'			=>	$_POST['g_container_lng'],
						'lat'			=>	$_POST['g_container_lat'],
						'addrtype'		=>	'集出空站',
						'unit'			=>	$unit,
					);
					$this->addaddr($depot);
				}
				$num1 = $this->checkaddr($_POST['g_full_container_depot'],$userid,$unit);
				if($num1==0){
					$full_depot = array(
						'user_id'    	=>	$userid,
						'consignee' 	=>	$_POST['g_full_container_man'],
						'address' 		=>	$_POST['g_full_container_depot'],
						'phone_tel'	=>	$_POST['g_full_container_tel'],
						'lng'			=>	$_POST['g_full_container_lng'],
						'lat'			=>	$_POST['g_full_container_lat'],
						'addrtype'		=>	'集出重站',
						'unit'			=>	$unit,
					);
					$this->addaddr($full_depot);
				}
				$num2 = $this->checkaddr($_POST['g_loading_place'],$userid,$unit);
				if($num2==0){
					$place = array(
						'user_id'    	=>	$userid,
						'consignee' 	=>	$_POST['g_loading_man'],
						'address' 		=>	$_POST['g_loading_place'],
						'phone_tel'	=>	$_POST['g_loading_tel'],
						'lng'			=>	$_POST['g_loading_lng'],
						'lat'			=>	$_POST['g_loading_lat'],
						'addrtype'		=>	'集出卸货',
						'unit'			=>	$unit,
					);
					$this->addaddr($place);
				}
			}

			/* 保存 */
			$rows = $this->_goodstrunk_mod->edit($id, $data);
			if ($this->_goodstrunk_mod->has_error())
			{
				$this->show_warning($this->_goodstrunk_mod->get_error(), '',   'index.php?app=goodstrunk&act=edit&id=' . $id);
				return;
			}
			$this->show_message('编辑成功',
                '继续编辑',   'index.php?app=goodstrunk&act=edit&id=' . $id
			);
		}
	}


	//拆分货物
	function choosetrunks(){
		$db = &db();
		$type = $this->visitor->get('type');
		$unit = 0;
		if($type==4){
			$unit = $this->visitor->get('unit');
			$sql = "select o_name from zys_operator where o_carrier=1 and o_tid=".$unit;
			$o_name = $db->getOne($sql);
		}
		$this->assign('o_name', $o_name);
		$this->assign('unit', $unit);
		$goodstrunksub=& m('goodstrunksub');

		$page = $this->_get_page();

		//集卡id
		$gtid=empty($_GET['gtid'])?0:intval($_GET['gtid']);


		$sql = "select gs.*,op.o_name,ws.ws_new from (zys_goods_trunk_sub as gs left join zys_operator as op on gs.g_carrier = op.o_tid) left join zys_waybill_sub_trunk as ws on ws.ws_gstid = gs.gs_tid where gs_gtid = ".$gtid ." group by gs.gs_tid";

		$trunks = $goodstrunksub->getAll($sql);

		$page['item_count'] = count($trunks);

		$this->_format_page($page);
		$this->assign('page_info', $page);

		$this->assign('gs_gtid', $gtid);

		$this->assign('trunks', $trunks);

		$this->display('goodstrunk.choosetrunks.html');
	}
	//拆箱处理
	function goodstrunkdone(){
		$db=&db();
		$arrs = isset($_POST['num']) ? $_POST['num'] : '';
		$checkarrs = isset($_POST['cnum']) ? $_POST['cnum'] : '';
		$gtid =isset($_POST['gs_gtid']) ? intval($_POST['gs_gtid']) : 0;
		$carrier =isset($_POST['g_carrier']) ? intval($_POST['g_carrier']) : 0;
		if(empty($carrier)){
			$this->show_warning('请选择承运商','','index.php?app=goodstrunk&act=edit&id='.$gtid);
			return;
		}
		if(empty($checkarrs)){
			$this->show_warning('请选择需要处理的集装箱','','index.php?app=goodstrunk&act=edit&id='.$gtid);
			return;
		}
		if($checkarrs!=null&&count($checkarrs)>0){
			foreach ($checkarrs as $k=>$v){
				$sql="select * from zys_goods_trunk_sub where gs_tid=".$k;
				$info=$db->getRow($sql);
				if(empty($info)){
					$this->show_warning('操作错误','','index.php?app=goodstrunk&act=edit&id='.$gtid);
					return;
				}
				if($arrs[$k] > $info['g_volume']){
					$this->show_warning('请重新拆分数据','','index.php?app=goodstrunk&act=edit&id='.$gtid);
					return;
				}elseif($arrs[$k] == $info['g_volume']){
					//update
					$sql="UPDATE zys_goods_trunk_sub SET g_carrier=".$carrier." WHERE gs_tid=$k";
					$db->query($sql);
				}else{
					//insert
					$data = array(
                        'gs_gtid'=>$gtid,
                        'g_box' => $info['g_box'],
                        'g_volume' => $arrs[$k],
                        'g_ctn_no'=>$info['g_ctn_no'],
                        'g_eir_code'=>$info['g_eir_code'],
                        'g_carrier'=>$carrier,
                        'g_gstid' => $k
					);
					$this->_goodstrunksub_mod->add($data);
					//update
					$volume = intval($info['g_volume'] - $arrs[$k]);
					$sql="UPDATE zys_goods_trunk_sub SET g_volume=".$volume." WHERE gs_tid=$k";
					$db->query($sql);
				}
				unset($info);
			}
		}
		echo("<script>window.parent.location.href='index.php?app=goodstrunk&act=edit&id=".$gtid."';</script>");
	}

	//取消提交货物
	function remove(){
		try{
			$gtid = isset($_POST['gtid']) ? intval($_POST['gtid']) : 0;
			if(empty($gtid)){
				throw new Exception('操作失败');
			}
			$db=&db();
			$sql = "select *  from zys_goods_trunk_sub where g_gstid !='' and gs_gtid=".$gtid;
			$data = $db->getAll($sql);

			if(!empty($data)){
				foreach($data as $val){
					$gstid = $val['g_gstid'];
					$num = $val['g_volume'];
					//update
					$sql = "UPDATE zys_goods_trunk_sub SET g_volume = g_volume + $num WHERE gs_tid=$gstid";
					$db->query($sql);
					//删除
					$delsql = "delete from zys_goods_trunk_sub where gs_tid = ".$val['gs_tid'];
					$db->query($delsql);
				}
			}
			$this->json_result(array(),'取消成功');

		}catch (Exception $e){
			$this->json_error($e->getMessage());
		}
	}

	//ajxs删除
	function del(){

			
		$id = isset($_GET['id']) ? trim($_GET['id']) : '';
			

		$this->_goodstrunksub_mod->drop($id);
	}

	//删除
	function drop(){
		$arr = isset($_GET['id']) ? trim($_GET['id']) : '';
		$db=&db();
		$arrs=explode(',', $arr);
		if($arrs!=null&&count($arrs)>0){
			foreach ($arrs as $v){
				$sql="SELECT n.*,m.user_name,IFNULL(x.ws_tid,0) as ws_tid FROM zys_goods_trunk n LEFT JOIN zys_member m ON n.g_optid=m.user_id
		LEFT JOIN (SELECT ws_tid,IFNULL(ws_gid,0) as ws_gid from zys_waybill_sub_trunk GROUP BY ws_gid) x on n.g_tid=ws_gid WHERE g_tid=".$v;
				$status=$db->getRow($sql);
				if($status&&$status['ws_tid']){

				}else{
					if (!$this->_goodstrunk_mod->drop($v)){
						$this->show_warning($this->_goodstrunk_mod->get_error(),'','index.php?app=goodstrunk');
						return;
					}
				}
			}
		}else{
			$sql="SELECT n.*,m.user_name,IFNULL(x.ws_tid,0) as ws_tid FROM zys_goods_trunk n LEFT JOIN zys_member m ON n.g_optid=m.user_id
		LEFT JOIN (SELECT ws_tid,IFNULL(ws_gid,0) as ws_gid from zys_waybill_sub_trunk GROUP BY ws_gid) x on n.g_tid=ws_gid WHERE g_tid=".$arr;
			$status=$db->getRow($sql);
			if($status&&$status['ws_tid']){
				$this->show_message('此货单不能删除','','index.php?app=goodstrunk');
				return ;
			}else{
				if (!$this->_goodstrunk_mod->drop($arr)){
					$this->show_warning($this->_goodstrunk_mod->get_error(),'','index.php?app=goodstrunk');
					return;
				}
			}
		}
		$this->show_message('删除成功','','index.php?app=goodstrunk');
			
	}

	//加入到地址管理系统中

	function addaddr($data){
		$addr = & m('addr');
		$addr->add($data);
	}
	//验证地址是否已经存在
	function checkaddr($addr,$userid,$unit){
		$db = &db();
		$sql="select count(*) as num from zys_addr where address='".$addr."' and user_id=".$userid." and unit=".$unit;
		$num = $db->getone($sql);
		return $num;
	}


	//拆分货物生成运单
	function addbygoodscheck() {
		$arr = isset($_GET['id']) ? trim($_GET['id']) : '';
		$db=&db();
		$arrs=explode(',', $arr);
		if($arrs!=null&&count($arrs)>0){
			$ids="";
			foreach ($arrs as $v){
				$ids.="'".$v."',";
			}
			$ids=trim($ids,',');
			if(empty($ids)){
				$this->show_message('您还没有选择货物','','index.php?app=goodstrunk');
				return ;
			}
			$sql="SELECT g_tid,g_status FROM zys_goods_trunk WHERE g_tid IN ($ids) and g_status='已提交'";
			$g=$db->getAll($sql);
			if ($g){
				$ids="";
				foreach ($g as $v){
					$ids.="'".$v['g_tid']."',";
				}
				$ids=trim($ids,',');
				if(!empty($ids)){
					$sql="SELECT s.*,t.g_type,t.g_ship,t.g_awb_code,t.g_voyage FROM zys_goods_trunk_sub s left join zys_goods_trunk t on s.gs_gtid=t.g_tid WHERE gs_gtid IN ($ids) ORDER BY g_box DESC";
					$goodssub=$db->getAll($sql);

					$opid=$this->visitor->get('user_id');
					$sql="SELECT o_tid,o_name FROM zys_operator WHERE o_code='0001' AND o_carrier=1";
					$carrier_id=0;
					$carrier="";
					$r=$db->getRow($sql);
					if($r){
						if($r['o_tid']){
							$carrier_id=$r['o_tid'];
						}
						if($r['o_name']){
							$carrier=$r['o_name'];
						}
					}
					if($goodssub){

						$temp_in=array();
						$temp_out=array();

						foreach ($goodssub as $v){
							if(strstr($v['g_box'],"40")){
								if($v['g_volume']){
									for ($i = 0; $i < $v['g_volume']; $i++) {
										$code=$this->autocode();
										$opdate=gmtime();

										$sql="insert into zys_waybill_main(wm_type,wm_code,wm_optid,wm_opdate,wm_ship,wm_voyage,wm_awb_code,wm_carrier,wm_carrier_id)
									value('".$v['g_type']."','".$code."','".$opid."','".$opdate."','".$v['g_ship']."','".$v['g_voyage']."','".$v['g_awb_code']."','".$carrier."','".$carrier_id."')";
										$db->query($sql);
										$wmid=$db->insert_id();
										if($wmid){
											$sql="insert into zys_waybill_sub_trunk (ws_wmtid,ws_gid,ws_gstid,ws_volume,ws_new)
										values('".$wmid."','".$v['gs_gtid']."','".$v['gs_tid']."','1','0')";
											$db->query($sql);

											$sql="update zys_goods_trunk set g_status='已交运' where g_tid=".$v['gs_gtid'];
											$db->query($sql);
										}
									}
								}
							}
							if(strstr($v['g_box'],"20")){
								if($v['g_volume']){
									if($v['g_volume']>=2){

										for ($i = 0; $i < intval($v['g_volume']/2); $i++) {
											$code=$this->autocode();
											$opdate=gmtime();

											$sql="insert into zys_waybill_main(wm_type,wm_code,wm_optid,wm_opdate,wm_ship,wm_voyage,wm_awb_code,wm_carrier,wm_carrier_id)
									value('".$v['g_type']."','".$code."','".$opid."','".$opdate."','".$v['g_ship']."','".$v['g_voyage']."','".$v['g_awb_code']."','".$carrier."','".$carrier_id."')";
											$db->query($sql);
											$wmid=$db->insert_id();
											if($wmid){
												$sql="insert into zys_waybill_sub_trunk (ws_wmtid,ws_gid,ws_gstid,ws_volume,ws_new)
											values('".$wmid."','".$v['gs_gtid']."','".$v['gs_tid']."','2','0')";
												$db->query($sql);

												$sql="update zys_goods_trunk set g_status='已交运' where g_tid=".$v['gs_gtid'];
												$db->query($sql);
											}
										}
										if($v['g_volume']%2!=0){
											$v['g_volume']=1;
											/*if($v['g_type']=='集港'){
												$temp_in[]=$v;
												}else if($v['g_type']=='疏港'){
												$temp_out[]=$v;
												}*/

											$code=$this->autocode();
											$opdate=gmtime();

											$sql="insert into zys_waybill_main(wm_type,wm_code,wm_optid,wm_opdate,wm_ship,wm_voyage,wm_awb_code,wm_carrier,wm_carrier_id)
									value('".$v['g_type']."','".$code."','".$opid."','".$opdate."','".$v['g_ship']."','".$v['g_voyage']."','".$v['g_awb_code']."','".$carrier."','".$carrier_id."')";
											$db->query($sql);
											$wmid=$db->insert_id();
											if($wmid){
												$sql="insert into zys_waybill_sub_trunk (ws_wmtid,ws_gid,ws_gstid,ws_volume,ws_new)
											values('".$wmid."','".$v['gs_gtid']."','".$v['gs_tid']."','1','0')";
												$db->query($sql);

												$sql="update zys_goods_trunk set g_status='已交运' where g_tid=".$v['gs_gtid'];
												$db->query($sql);
											}

										}

									}else if($v['g_volume']==1){
										/*if($v['g_type']=='集港'){
											$temp_in[]=$v;
											}else if($v['g_type']=='疏港'){
											$temp_out[]=$v;
											}*/

										$code=$this->autocode();
										$opdate=gmtime();

										$sql="insert into zys_waybill_main(wm_type,wm_code,wm_optid,wm_opdate,wm_ship,wm_voyage,wm_awb_code,wm_carrier,wm_carrier_id)
									value('".$v['g_type']."','".$code."','".$opid."','".$opdate."','".$v['g_ship']."','".$v['g_voyage']."','".$v['g_awb_code']."','".$carrier."','".$carrier_id."')";
										$db->query($sql);
										$wmid=$db->insert_id();
										if($wmid){
											$sql="insert into zys_waybill_sub_trunk (ws_wmtid,ws_gid,ws_gstid,ws_volume,ws_new)
											values('".$wmid."','".$v['gs_gtid']."','".$v['gs_tid']."','1','0')";
											$db->query($sql);

											$sql="update zys_goods_trunk set g_status='已交运' where g_tid=".$v['gs_gtid'];
											$db->query($sql);
										}
									}

								}
							}


						}

						/*if($temp_in&&count($temp_in)>0){
							if(count($temp_in)>=2){
							for ($i = 0; $i < intval(count($temp_in)/2); $i++) {
							$code=$this->autocode();
							$opdate=gmtime();

							$sql="insert into zys_waybill_main(wm_type,wm_code,wm_optid,wm_opdate,wm_ship,wm_voyage,wm_awb_code,wm_carrier,wm_carrier_id)
							value('".$temp_in[$i]['g_type']."','".$code."','".$opid."','".$opdate."',
							'".$temp_in[$i*2+$ii]['g_ship']."','".$temp_in[$i*2+$ii]['g_voyage']."','".$temp_in[$i*2+$ii]['g_awb_code']."','".$carrier."','".$carrier_id."')";
							$db->query($sql);
							$wmid=$db->insert_id();
							if($wmid){
							for ($ii = 0; $ii < 2; $ii++) {
							$sql="insert into zys_waybill_sub_trunk (ws_wmtid,ws_gid,ws_gstid,ws_volume,ws_new)
							values('".$wmid."','".$temp_in[$i*2+$ii]['gs_gtid']."','".$temp_in[$i*2+$ii]['gs_tid']."','1','0')";
							$db->query($sql);

							$sql="update zys_goods_trunk set g_status='已交运' where g_tid=".$temp_in[$i*2+$ii]['gs_gtid'];
							$db->query($sql);
							}

							}
							}
							if(count($temp_in)%2!=0){
							$code=$this->autocode();
							$opdate=gmtime();

							$sql="insert into zys_waybill_main(wm_type,wm_code,wm_optid,wm_opdate,wm_ship,wm_voyage,wm_awb_code,wm_carrier,wm_carrier_id)
							value('".$temp_in[count($temp_in)-1]['g_type']."','".$code."','".$opid."','".$opdate."',
							'".$temp_in[count($temp_in)-1]['g_ship']."','".$temp_in[count($temp_in)-1]['g_voyage']."','".$temp_in[$i*2+$ii]['g_awb_code']."','".$carrier."','".$carrier_id."')";
							$db->query($sql);
							$wmid=$db->insert_id();
							if($wmid){
							$sql="insert into zys_waybill_sub_trunk (ws_wmtid,ws_gid,ws_gstid,ws_volume,ws_new)
							values('".$wmid."','".$temp_in[count($temp_in)-1]['gs_gtid']."','".$temp_in[count($temp_in)-1]['gs_tid']."','1','0')";
							$db->query($sql);

							$sql="update zys_goods_trunk set g_status='已交运' where g_tid=".$temp_in[count($temp_in)-1]['gs_gtid'];
							$db->query($sql);

							}
							}
							}else if(count($temp_in)==1){
							$code=$this->autocode();
							$opdate=gmtime();

							$sql="insert into zys_waybill_main(wm_type,wm_code,wm_optid,wm_opdate,wm_ship,wm_voyage,wm_awb_code,wm_carrier,wm_carrier_id)
							value('".$temp_in[0]['g_type']."','".$code."','".$opid."','".$opdate."',
							'".$temp_in[0]['g_ship']."','".$temp_in[0]['g_voyage']."','".$temp_in[0]['g_awb_code']."','".$carrier."','".$carrier_id."')";
							$db->query($sql);
							$wmid=$db->insert_id();
							if($wmid){
							$sql="insert into zys_waybill_sub_trunk (ws_wmtid,ws_gid,ws_gstid,ws_volume,ws_new)
							values('".$wmid."','".$temp_in[0]['gs_gtid']."','".$temp_in[0]['gs_tid']."','1','0')";
							$db->query($sql);

							$sql="update zys_goods_trunk set g_status='已交运' where g_tid=".$temp_in[0]['gs_gtid'];
							$db->query($sql);

							}
							}

							}


							if($temp_out&&count($temp_out)>0){
							if(count($temp_out)>=2){
							for ($i = 0; $i < intval(count($temp_out)/2); $i++) {
							$code=$this->autocode();
							$opdate=gmtime();

							$sql="insert into zys_waybill_main(wm_type,wm_code,wm_optid,wm_opdate,wm_ship,wm_voyage,wm_awb_code,wm_carrier,wm_carrier_id)
							value('".$temp_out[$i]['g_type']."','".$code."','".$opid."','".$opdate."',
							'".$temp_out[$i*2+$ii]['g_ship']."','".$temp_out[$i*2+$ii]['g_voyage']."','".$temp_out[$i*2+$ii]['g_awb_code']."','".$carrier."','".$carrier_id."')";
							$db->query($sql);
							$wmid=$db->insert_id();
							if($wmid){
							for ($ii = 0; $ii < 2; $ii++) {
							$sql="insert into zys_waybill_sub_trunk (ws_wmtid,ws_gid,ws_gstid,ws_volume,ws_new)
							values('".$wmid."','".$temp_out[$i*2+$ii]['gs_gtid']."','".$temp_out[$i*2+$ii]['gs_tid']."','1','0')";
							$db->query($sql);

							$sql="update zys_goods_trunk set g_status='已交运' where g_tid=".$temp_out[$i*2+$ii]['gs_gtid'];
							$db->query($sql);
							}

							}
							}
							if(count($temp_out)%2!=0){
							$code=$this->autocode();
							$opdate=gmtime();

							$sql="insert into zys_waybill_main(wm_type,wm_code,wm_optid,wm_opdate,wm_ship,wm_voyage,wm_awb_code,wm_carrier,wm_carrier_id)
							value('".$temp_out[count($temp_out)-1]['g_type']."','".$code."','".$opid."','".$opdate."',
							'".$temp_out[count($temp_out)-1]['g_ship']."','".$temp_out[count($temp_out)-1]['g_voyage']."','".$temp_out[$i*2+$ii]['g_awb_code']."','".$carrier."','".$carrier_id."')";
							$db->query($sql);
							$wmid=$db->insert_id();
							if($wmid){
							$sql="insert into zys_waybill_sub_trunk (ws_wmtid,ws_gid,ws_gstid,ws_volume,ws_new)
							values('".$wmid."','".$temp_out[count($temp_out)-1]['gs_gtid']."','".$temp_out[count($temp_out)-1]['gs_tid']."','1','0')";
							$db->query($sql);

							$sql="update zys_goods_trunk set g_status='已交运' where g_tid=".$temp_out[count($temp_out)-1]['gs_gtid'];
							$db->query($sql);

							}
							}
							}else if(count($temp_out)==1){
							$code=$this->autocode();
							$opdate=gmtime();

							$sql="insert into zys_waybill_main(wm_type,wm_code,wm_optid,wm_opdate,wm_ship,wm_voyage,wm_awb_code,wm_carrier,wm_carrier_id)
							value('".$temp_out[0]['g_type']."','".$code."','".$opid."','".$opdate."',
							'".$temp_out[0]['g_ship']."','".$temp_out[0]['g_voyage']."','".$temp_out[0]['g_awb_code']."','".$carrier."','".$carrier_id."')";
							$db->query($sql);
							$wmid=$db->insert_id();
							if($wmid){
							$sql="insert into zys_waybill_sub_trunk (ws_wmtid,ws_gid,ws_gstid,ws_volume,ws_new)
							values('".$wmid."','".$temp_out[0]['gs_gtid']."','".$temp_out[0]['gs_tid']."','1','0')";
							$db->query($sql);

							$sql="update zys_goods_trunk set g_status='已交运' where g_tid=".$temp_out[0]['gs_gtid'];
							$db->query($sql);

							}
							}

							}*/

					}
				}
			}

		}else{
			$this->show_message('您还没有选择货物','','index.php?app=goodstrunk');
		}
		$this->show_message('发车成功','','index.php?app=goodstrunk');
	}

	//拆分全部货物生成运单
	function addbygoodsall() {
		$db=&db();
		$sql="SELECT g_tid,g_status FROM zys_goods_trunk WHERE  g_status='已提交'";
		$g=$db->getAll($sql);
		if ($g){
			$ids="";
			foreach ($g as $v){
				$ids.="'".$v['g_tid']."',";
			}
			$ids=trim($ids,',');
			if(!empty($ids)){
				$sql="SELECT s.*,t.g_type,t.g_ship,t.g_awb_code,t.g_voyage FROM zys_goods_trunk_sub s left join zys_goods_trunk t on s.gs_gtid=t.g_tid WHERE gs_gtid IN ($ids) ORDER BY g_box DESC";
				$goodssub=$db->getAll($sql);

				$opid=$this->visitor->get('user_id');
				$sql="SELECT o_tid,o_name FROM zys_operator WHERE o_code='0001' AND o_carrier=1";
				$carrier_id=0;
				$carrier="";
				$r=$db->getRow($sql);
				if($r){
					if($r['o_tid']){
						$carrier_id=$r['o_tid'];
					}
					if($r['o_name']){
						$carrier=$r['o_name'];
					}
				}
				if($goodssub){

					$temp_in=array();
					$temp_out=array();

					foreach ($goodssub as $v){
						if(strstr($v['g_box'],"40")){
							if($v['g_volume']){
								for ($i = 0; $i < $v['g_volume']; $i++) {
									$code=$this->autocode();
									$opdate=gmtime();

									$sql="insert into zys_waybill_main(wm_type,wm_code,wm_optid,wm_opdate,wm_ship,wm_voyage,wm_awb_code,wm_carrier,wm_carrier_id)
									value('".$v['g_type']."','".$code."','".$opid."','".$opdate."','".$v['g_ship']."','".$v['g_voyage']."','".$v['g_awb_code']."','".$carrier."','".$carrier_id."')";
									$db->query($sql);
									$wmid=$db->insert_id();
									if($wmid){
										$sql="insert into zys_waybill_sub_trunk (ws_wmtid,ws_gid,ws_gstid,ws_volume,ws_new)
										values('".$wmid."','".$v['gs_gtid']."','".$v['gs_tid']."','1','0')";
										$db->query($sql);

										$sql="update zys_goods_trunk set g_status='已交运' where g_tid=".$v['gs_gtid'];
										$db->query($sql);
									}
								}
							}
						}
						if(strstr($v['g_box'],"20")){
							if($v['g_volume']){
								if($v['g_volume']>=2){

									for ($i = 0; $i < intval($v['g_volume']/2); $i++) {
										$code=$this->autocode();
										$opdate=gmtime();

										$sql="insert into zys_waybill_main(wm_type,wm_code,wm_optid,wm_opdate,wm_ship,wm_voyage,wm_awb_code,wm_carrier,wm_carrier_id)
									value('".$v['g_type']."','".$code."','".$opid."','".$opdate."','".$v['g_ship']."','".$v['g_voyage']."','".$v['g_awb_code']."','".$carrier."','".$carrier_id."')";
										$db->query($sql);
										$wmid=$db->insert_id();
										if($wmid){
											$sql="insert into zys_waybill_sub_trunk (ws_wmtid,ws_gid,ws_gstid,ws_volume,ws_new)
											values('".$wmid."','".$v['gs_gtid']."','".$v['gs_tid']."','2','0')";
											$db->query($sql);

											$sql="update zys_goods_trunk set g_status='已交运' where g_tid=".$v['gs_gtid'];
											$db->query($sql);
										}
									}
									if($v['g_volume']%2!=0){
										$v['g_volume']=1;
										/*if($v['g_type']=='集港'){
										 $temp_in[]=$v;
										 }else if($v['g_type']=='疏港'){
										 $temp_out[]=$v;
										 }*/

										$code=$this->autocode();
										$opdate=gmtime();

										$sql="insert into zys_waybill_main(wm_type,wm_code,wm_optid,wm_opdate,wm_ship,wm_voyage,wm_awb_code,wm_carrier,wm_carrier_id)
									value('".$v['g_type']."','".$code."','".$opid."','".$opdate."','".$v['g_ship']."','".$v['g_voyage']."','".$v['g_awb_code']."','".$carrier."','".$carrier_id."')";
										$db->query($sql);
										$wmid=$db->insert_id();
										if($wmid){
											$sql="insert into zys_waybill_sub_trunk (ws_wmtid,ws_gid,ws_gstid,ws_volume,ws_new)
											values('".$wmid."','".$v['gs_gtid']."','".$v['gs_tid']."','1','0')";
											$db->query($sql);

											$sql="update zys_goods_trunk set g_status='已交运' where g_tid=".$v['gs_gtid'];
											$db->query($sql);
										}

									}

								}else if($v['g_volume']==1){
									/*if($v['g_type']=='集港'){
									 $temp_in[]=$v;
									 }else if($v['g_type']=='疏港'){
									 $temp_out[]=$v;
									 }*/

									$code=$this->autocode();
									$opdate=gmtime();

									$sql="insert into zys_waybill_main(wm_type,wm_code,wm_optid,wm_opdate,wm_ship,wm_voyage,wm_awb_code,wm_carrier,wm_carrier_id)
									value('".$v['g_type']."','".$code."','".$opid."','".$opdate."','".$v['g_ship']."','".$v['g_voyage']."','".$v['g_awb_code']."','".$carrier."','".$carrier_id."')";
									$db->query($sql);
									$wmid=$db->insert_id();
									if($wmid){
										$sql="insert into zys_waybill_sub_trunk (ws_wmtid,ws_gid,ws_gstid,ws_volume,ws_new)
											values('".$wmid."','".$v['gs_gtid']."','".$v['gs_tid']."','1','0')";
										$db->query($sql);

										$sql="update zys_goods_trunk set g_status='已交运' where g_tid=".$v['gs_gtid'];
										$db->query($sql);
									}
								}

							}
						}


					}
					/*
					 if($temp_in&&count($temp_in)>0){
						if(count($temp_in)>=2){
						for ($i = 0; $i < intval(count($temp_in)/2); $i++) {
						$code=$this->autocode();
						$opid=$this->visitor->get('user_id');
						$opdate=gmtime();

						$sql="insert into zys_waybill_main(wm_type,wm_code,wm_optid,wm_opdate,wm_ship,wm_voyage,wm_awb_code)
						value('".$temp_in[$i]['g_type']."','".$code."','".$opid."','".$opdate."',
						'".$temp_in[$i*2+$ii]['g_ship']."','".$temp_in[$i*2+$ii]['g_voyage']."','".$temp_in[$i*2+$ii]['g_awb_code']."')";
						$db->query($sql);
						$wmid=$db->insert_id();
						if($wmid){
						for ($ii = 0; $ii < 2; $ii++) {
						$sql="insert into zys_waybill_sub_trunk (ws_wmtid,ws_gid,ws_gstid,ws_volume,ws_new)
						values('".$wmid."','".$temp_in[$i*2+$ii]['gs_gtid']."','".$temp_in[$i*2+$ii]['gs_tid']."','1','0')";
						$db->query($sql);

						$sql="update zys_goods_trunk set g_status='已交运' where g_tid=".$temp_in[$i*2+$ii]['gs_gtid'];
						$db->query($sql);
						}

						}
						}
						if(count($temp_in)%2!=0){
						$code=$this->autocode();
						$opid=$this->visitor->get('user_id');
						$opdate=gmtime();

						$sql="insert into zys_waybill_main(wm_type,wm_code,wm_optid,wm_opdate,wm_ship,wm_voyage,wm_awb_code)
						value('".$temp_in[count($temp_in)-1]['g_type']."','".$code."','".$opid."','".$opdate."',
						'".$temp_in[count($temp_in)-1]['g_ship']."','".$temp_in[count($temp_in)-1]['g_voyage']."','".$temp_in[$i*2+$ii]['g_awb_code']."')";
						$db->query($sql);
						$wmid=$db->insert_id();
						if($wmid){
						$sql="insert into zys_waybill_sub_trunk (ws_wmtid,ws_gid,ws_gstid,ws_volume,ws_new)
						values('".$wmid."','".$temp_in[count($temp_in)-1]['gs_gtid']."','".$temp_in[count($temp_in)-1]['gs_tid']."','1','0')";
						$db->query($sql);

						$sql="update zys_goods_trunk set g_status='已交运' where g_tid=".$temp_in[count($temp_in)-1]['gs_gtid'];
						$db->query($sql);

						}
						}
						}else if(count($temp_in)==1){
						$code=$this->autocode();
						$opid=$this->visitor->get('user_id');
						$opdate=gmtime();

						$sql="insert into zys_waybill_main(wm_type,wm_code,wm_optid,wm_opdate,wm_ship,wm_voyage,wm_awb_code)
						value('".$temp_in[0]['g_type']."','".$code."','".$opid."','".$opdate."',
						'".$temp_in[0]['g_ship']."','".$temp_in[0]['g_voyage']."','".$temp_in[0]['g_awb_code']."')";
						$db->query($sql);
						$wmid=$db->insert_id();
						if($wmid){
						$sql="insert into zys_waybill_sub_trunk (ws_wmtid,ws_gid,ws_gstid,ws_volume,ws_new)
						values('".$wmid."','".$temp_in[0]['gs_gtid']."','".$temp_in[0]['gs_tid']."','1','0')";
						$db->query($sql);

						$sql="update zys_goods_trunk set g_status='已交运' where g_tid=".$temp_in[0]['gs_gtid'];
						$db->query($sql);

						}
						}

						}


						if($temp_out&&count($temp_out)>0){
						if(count($temp_out)>=2){
						for ($i = 0; $i < intval(count($temp_out)/2); $i++) {
						$code=$this->autocode();
						$opid=$this->visitor->get('user_id');
						$opdate=gmtime();

						$sql="insert into zys_waybill_main(wm_type,wm_code,wm_optid,wm_opdate,wm_ship,wm_voyage,wm_awb_code)
						value('".$temp_out[$i]['g_type']."','".$code."','".$opid."','".$opdate."',
						'".$temp_out[$i*2+$ii]['g_ship']."','".$temp_out[$i*2+$ii]['g_voyage']."','".$temp_out[$i*2+$ii]['g_awb_code']."')";
						$db->query($sql);
						$wmid=$db->insert_id();
						if($wmid){
						for ($ii = 0; $ii < 2; $ii++) {
						$sql="insert into zys_waybill_sub_trunk (ws_wmtid,ws_gid,ws_gstid,ws_volume,ws_new)
						values('".$wmid."','".$temp_out[$i*2+$ii]['gs_gtid']."','".$temp_out[$i*2+$ii]['gs_tid']."','1','0')";
						$db->query($sql);

						$sql="update zys_goods_trunk set g_status='已交运' where g_tid=".$temp_out[$i*2+$ii]['gs_gtid'];
						$db->query($sql);
						}

						}
						}
						if(count($temp_out)%2!=0){
						$code=$this->autocode();
						$opid=$this->visitor->get('user_id');
						$opdate=gmtime();

						$sql="insert into zys_waybill_main(wm_type,wm_code,wm_optid,wm_opdate,wm_ship,wm_voyage,wm_awb_code)
						value('".$temp_out[count($temp_out)-1]['g_type']."','".$code."','".$opid."','".$opdate."',
						'".$temp_out[count($temp_out)-1]['g_ship']."','".$temp_out[count($temp_out)-1]['g_voyage']."','".$temp_out[$i*2+$ii]['g_awb_code']."')";
						$db->query($sql);
						$wmid=$db->insert_id();
						if($wmid){
						$sql="insert into zys_waybill_sub_trunk (ws_wmtid,ws_gid,ws_gstid,ws_volume,ws_new)
						values('".$wmid."','".$temp_out[count($temp_out)-1]['gs_gtid']."','".$temp_out[count($temp_out)-1]['gs_tid']."','1','0')";
						$db->query($sql);

						$sql="update zys_goods_trunk set g_status='已交运' where g_tid=".$temp_out[count($temp_out)-1]['gs_gtid'];
						$db->query($sql);

						}
						}
						}else if(count($temp_out)==1){
						$code=$this->autocode();
						$opid=$this->visitor->get('user_id');
						$opdate=gmtime();

						$sql="insert into zys_waybill_main(wm_type,wm_code,wm_optid,wm_opdate,wm_ship,wm_voyage,wm_awb_code)
						value('".$temp_out[0]['g_type']."','".$code."','".$opid."','".$opdate."',
						'".$temp_out[0]['g_ship']."','".$temp_out[0]['g_voyage']."','".$temp_out[0]['g_awb_code']."')";
						$db->query($sql);
						$wmid=$db->insert_id();
						if($wmid){
						$sql="insert into zys_waybill_sub_trunk (ws_wmtid,ws_gid,ws_gstid,ws_volume,ws_new)
						values('".$wmid."','".$temp_out[0]['gs_gtid']."','".$temp_out[0]['gs_tid']."','1','0')";
						$db->query($sql);

						$sql="update zys_goods_trunk set g_status='已交运' where g_tid=".$temp_out[0]['gs_gtid'];
						$db->query($sql);

						}
						}

						}*/

				}
			}
		}
		$this->show_message('发车成功','','index.php?app=goodstrunk');
	}

}